import { createRouter, createWebHistory } from 'vue-router';
import Home from '../views/Home.vue';
import Login from '../views/Auth/Login.vue';
import Register from '../views/Auth/Register.vue';
import ArticleList from '../views/Article/List.vue';
import ArticleDetail from '../views/Article/Detail.vue';
import ArticleCreate from '../views/Article/Create.vue';

const routes = [
  { path: '/', component: Home },
  { path: '/login', component: Login },
  { path: '/register', component: Register },
  { path: '/articles', component: ArticleList },
  { path: '/articles/:id', component: ArticleDetail },
  { path: '/articles/create', component: ArticleCreate, meta: { requiresAuth: true } }
];

const router = createRouter({
  history: createWebHistory(),
  routes
});

// 路由守卫 - 登录验证
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('token');
  if (to.meta.requiresAuth && !isAuthenticated) {
    next('/login');
  } else {
    next();
  }
});

export default router;